package com.naiterui.ehp.bs.esign.controller.inner;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.naiterui.ehp.bp.bo.esign.DigitalCertificateBO;
import com.naiterui.ehp.bp.bo.esign.EsignNoSecretBO;
import com.naiterui.ehp.bp.bo.esign.EsignPharmacistBO;
import com.naiterui.ehp.bp.bo.esign.NoSecretParamBO;
import com.naiterui.ehp.bp.bo.esign.PreSignResultBO;
import com.naiterui.ehp.bp.bo.esign.WillTypeBO;
import com.naiterui.ehp.bp.support.exception.BusinessException;
import com.naiterui.ehp.bp.support.exception.PublicService;
import com.naiterui.ehp.bp.support.exception.ResponseVO;
import com.naiterui.ehp.bp.support.utils.JsonMapper;
import com.naiterui.ehp.bs.esign.api.bjca.vo.SignUserParamVO;
import com.naiterui.ehp.bs.esign.exception.ExceptionCodes;
import com.naiterui.ehp.bs.esign.service.IPharmacistService;

/**
 * @author guoyongxiang
 * @date 2019/12/5 13:13
 * @since 1.0.0
 */
@RequestMapping("esign/inner/pharmacist")
@RestController
public class EsignInnerPharmacistController {

    private static final Logger LOGGER = LoggerFactory.getLogger(EsignInnerPharmacistController.class);

    @Autowired
    private IPharmacistService pharmacistService;

    /**
     * 注册电子签名用户-医生
     *
     * @param signUserParamVO
     *
     * @return
     *
     * @throws BusinessException
     */

    @PostMapping("esign/ph/add")
    public ResponseVO<Void> eSignUserAdd(SignUserParamVO signUserParamVO) throws BusinessException {
        LOGGER.info("请求创建电子签名药师用户 pharmacistId:{}", signUserParamVO.getUserId());

        pharmacistService.addUserPh(signUserParamVO);
        return PublicService.returnResponseVO(ExceptionCodes.SUCCESS);
    }

    /**
     * <pre>
     *     发起实名认证
     * </pre>
     *
     * @param name   姓名
     * @param idCard 身份证号
     * @param phone  手机号
     *
     * @return
     *
     * @throws BusinessException
     */
    @PostMapping("esign/realname/code")
    public ResponseVO<Void> realnameCode(String name, String phone, String idCard) throws BusinessException {
        LOGGER.info("发起短信实名认证 phone:{}", phone);
        pharmacistService.getRealnameCode(name, phone, idCard);
        return PublicService.returnResponseVO(ExceptionCodes.SUCCESS);
    }

    /**
     * <pre>
     *     实名认证校验
     * </pre>
     *
     * @param phone 手机号
     * @param code  验证码
     *
     * @return
     *
     * @throws BusinessException
     */
    @PostMapping("esign/realname/check")
    public ResponseVO<Boolean> readlnameCheck(String phone, String code) throws BusinessException {
        LOGGER.info("发起短信实名认证 phone:{}", phone);
        pharmacistService.checkRealname(phone, code);
        return PublicService.returnResponseVO(true);
    }


    /**
     * 获取药师签名账号信息
     *
     * @param pharmacistId
     *
     * @return
     *
     * @throws BusinessException
     */
    @PostMapping("esign/info")
    public ResponseVO<EsignPharmacistBO> getEsignInfo(Long pharmacistId) throws BusinessException {
        LOGGER.info("获取药师签名账号信息：pharmacistId={}", pharmacistId);
        EsignPharmacistBO bo = pharmacistService.getEsignInfo(pharmacistId);
        return PublicService.returnResponseVO(bo);
    }

    /**
     * 更新药师签名图片
     *
     * @param pharmacistId
     *
     * @return
     *
     * @throws BusinessException
     */
    @PostMapping("esign/seal/update")
    public ResponseVO<Void> getEsignInfo(Long pharmacistId, String sealImage) throws BusinessException {
        LOGGER.info("更新药师签名图片：pharmacistId={}", pharmacistId);
        pharmacistService.updateSealData(pharmacistId, sealImage);
        return PublicService.returnResponseVO(ExceptionCodes.SUCCESS);
    }

    /**
     * 获取意愿签署方式列表
     * @return
     * @throws BusinessException
     */
    @PostMapping("will/types")
    public ResponseVO<List<WillTypeBO>> getWillTypes(Long pharmacistId) throws BusinessException {
        LOGGER.info("药师获取意愿签署方式列表: pharmacistId:{}", pharmacistId);
        return PublicService.returnResponseVO(pharmacistService.getWillTypes(pharmacistId));
    }

    /**
     * 更新意愿签署方式
     *
     * @param pharmacistId
     *
     * @return
     *
     * @throws BusinessException
     */
    @PostMapping("/will/type/update")
    public ResponseVO<Void> updateWillType(Long pharmacistId, String willType) throws BusinessException {
        LOGGER.info("更新签名密码：pharmacistId={}, willType={}", pharmacistId, willType);
        pharmacistService.updateWillType(pharmacistId, willType);
        return PublicService.returnResponseVO(ExceptionCodes.SUCCESS);
    }

    /**
     * 更新签名密码
     *
     * @param pharmacistId
     *
     * @return
     *
     * @throws BusinessException
     */
    @PostMapping("/password/update")
    public ResponseVO<Void> updateSignPassword(Long pharmacistId, String password) throws BusinessException {
        LOGGER.info("更新签名密码：doctorId={}, password={}", pharmacistId, password);
        pharmacistService.updateSignPassword(pharmacistId, password);
        return PublicService.returnResponseVO(ExceptionCodes.SUCCESS);
    }

    @GetMapping("/getNoSecret")
    public ResponseVO<Integer> getNoSecret(Long pharmacistId) throws BusinessException {
        LOGGER.info("获取免密状态：pharmacistId={}", pharmacistId);
        return PublicService.returnResponseVO(pharmacistService.getNoSecretByPharmacistId(pharmacistId));
    }

    @GetMapping("/getNoSecretInfo")
    public ResponseVO<EsignNoSecretBO> getNoSecretInfo(Long pharmacistId) throws BusinessException {
        LOGGER.info("获取免密状态信息：pharmacistId={}", pharmacistId);
        return PublicService.returnResponseVO(pharmacistService.getEsignNoSecretByPharmacistId(pharmacistId));
    }

    @PostMapping("/preNoSecret")
    public ResponseVO<PreSignResultBO> preNoSecret(Long pharmacistId) throws BusinessException {
        LOGGER.info("药师设置免密预签署：{}", pharmacistId);
        return PublicService.returnResponseVO(pharmacistService.preNoSecret(pharmacistId));
    }

    @PostMapping("/setNoSecret")
    public ResponseVO<Void> setNoSecret(@RequestBody NoSecretParamBO paramBO) throws BusinessException {
        LOGGER.info("药师设置免密：{}", JsonMapper.toJson(paramBO));
        // pharmacistService.setNoSecret(pharmacistId, noSecret, authPwd);
        pharmacistService.setNoSecret(paramBO);
        return PublicService.returnResponseVO(ExceptionCodes.SUCCESS);
    }

    /**
     * 获取药师数字证书
     * 
     * @param pharmacistId
     * @return
     * @throws BusinessException
     */
    @GetMapping("/getDigitalCertificate")
    public DigitalCertificateBO getDigitalCertificate(Long pharmacistId) throws BusinessException {
        return pharmacistService.getDigitalCertificate(pharmacistId);
    }

    @GetMapping("/saveSealImg")
    public ResponseVO<Void> saveSealImg(Long pharmacistId, String sealImg) throws BusinessException {
        LOGGER.info("更新签名用户签章图片：doctorId={}, sealImg={}", pharmacistId, sealImg);
        pharmacistService.saveSealImg(pharmacistId, sealImg);
        return PublicService.returnResponseVO(ExceptionCodes.SUCCESS);
    }

    @GetMapping("/checkPharmacistRealname")
    public ResponseVO<Void> checkPharmacistRealname(Long pharmacistId, String idCard) throws BusinessException {
        LOGGER.info("检查该药师身份证是否已实名 pharmacistId: {}, idCard:{}", pharmacistId, idCard);
        pharmacistService.checkPharmacistRealname(idCard);
        return PublicService.returnResponseVO(ExceptionCodes.SUCCESS);
    }

}
